<?php // $Id: settings.php,v 1.60 2009/07/09 07:35:08 tjhunt Exp $

require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once('edit_form.php');
require_once('locallib.php');

global $DB;

$section = 'modsettingbasiclti';
$return = optional_param('return','', PARAM_ALPHA);
$adminediting = optional_param('adminedit', -1, PARAM_BOOL);
$action       = optional_param('action',null, PARAM_TEXT);
$id 	      = optional_param('id',null, PARAM_INT);

/// no guest autologin
require_login(0, false);
$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
$PAGE->set_url($CFG->wwwroot . 'mod/basiclti/typessettings.php', array('section' => $section));
$PAGE->set_pagetype('admin-setting-' . $section);

$adminroot = admin_get_root(); // need all settings
$settingspage = $adminroot->locate($section, true);

if (empty($settingspage) or !($settingspage instanceof admin_settingpage)) {
    print_error('sectionerror', 'admin', "$CFG->wwwroot/$CFG->admin/");
    die;
}

if (!($settingspage->check_access())) {
    print_error('accessdenied', 'admin');
    die;
}

/// WRITING SUBMITTED DATA (IF ANY) -------------------------------------------------------------------------------

$statusmsg = '';
$errormsg  = '';
$focus = '';

if ($data = data_submitted() and confirm_sesskey()) {
	if (isset($id)){
		$type = new StdClass();
		$type->id = $id;
		$type->name = $data->lti_typename;
		$type->rawname = eregi_replace('[^a-zA-Z]', '', $type->name);
	    if ($DB->update_record('basiclti_types', $type)) {
			unset ($data->lti_typename);
			//@TODO: update work
			foreach ($data as $key => $value){
				if (substr($key,0,4)=='lti_' && !empty($value)){
					$record = new StdClass();
					$record->typeid = $id;
					$record->name = substr($key,4);
					$record->value = $value;
					if (basiclti_update_config($record)){
				        $statusmsg = get_string('changessaved');
					} else {
				        $errormsg = get_string('errorwithsettings', 'admin');
					}
				}
		    }
	    }
	} else {
		$type = new StdClass();
		$type->name = $data->lti_typename;
		$type->rawname = eregi_replace('[^a-zA-Z]', '', $type->name);
		if ($id = $DB->insert_record('basiclti_types', $type)){
			unset ($data->lti_typename);
		    foreach ($data as $key => $value){
				if (substr($key,0,4)=='lti_' && !empty($value)){
					$record = new StdClass();
					$record->typeid = $id;
					$record->name = substr($key,4);
					$record->value = $value;
					if (basiclti_add_config($record)){
				        $statusmsg = get_string('changessaved');
					} else {
				        $errormsg = get_string('errorwithsettings', 'admin');
					}
				}
		    }
		} else {
	        $errormsg = get_string('errorwithsettings', 'admin');
		}
	}
    if (empty($adminroot->errors)) {
        switch ($return) {
            case 'site':  redirect("$CFG->wwwroot/");
            case 'admin': redirect("$CFG->wwwroot/$CFG->admin/");
        }
    } else {
        $errormsg = get_string('errorwithsettings', 'admin');
        $firsterror = reset($adminroot->errors);
        $focus = $firsterror->id;
    }
    $adminroot =& admin_get_root(true); //reload tree
    $page      =& $adminroot->locate($section);
}

if ($PAGE->user_allowed_editing() && $adminediting != -1) {
    $USER->editing = $adminediting;
}


if ($PAGE->user_allowed_editing()) {
    $options = $PAGE->url->params();
    if ($PAGE->user_is_editing()) {
        $caption = get_string('blockseditoff');
        $options['adminedit'] = 'off';
    } else {
        $caption = get_string('blocksediton');
        $options['adminedit'] = 'on';
    }
    $buttons = print_single_button($PAGE->url->out(false), $options, $caption, 'get', '', true);
}

$visiblepathtosection = array_reverse($settingspage->visiblepath);
$navlinks = array();
foreach ($visiblepathtosection as $element) {
    $navlinks[] = array('name' => $element, 'link' => null, 'type' => 'misc');
}
$navigation = build_navigation($navlinks);

print_header("$SITE->shortname: " . implode(": ",$visiblepathtosection), $SITE->fullname, $navigation, $focus, '', true, $buttons, '');

if ($errormsg !== '') {
    notify ($errormsg);

} else if ($statusmsg !== '') {
    notify ($statusmsg, 'notifysuccess');
}

// ---------------------------------------------------------------------------------------------------------------


print_heading($settingspage->visiblename);

if ($action == 'add'){
	$form = new mod_basiclti_edit_types_form();
	$form->display();
} else if ($action == 'update'){
	$form = new mod_basiclti_edit_types_form('typessettings.php?id='.$id);
	$type = basiclti_get_type_type_config($id);
	$form->set_data($type);
	$form->display();
} else if ($action == 'delete'){
	basiclti_delete_type($id);
	echo '<fieldset>';
	echo '<h4 class="main"><a href="typessettings.php?action=add&amp;sesskey='.$USER->sesskey.'">'.get_string('addtype','basiclti').'</a></h4>';
	basiclti_filter_print_types();
	echo '</fieldset>';
	echo '<div class="mdl-align"><a href="'.$CFG->wwwroot.'/admin/settings.php?section=modsettingbasiclti">'.get_string('back').'</a></div>';

} else {
	echo '<fieldset>';
	echo '<h4 class="main"><a href="typessettings.php?action=add&amp;sesskey='.$USER->sesskey.'">'.get_string('addtype','basiclti').'</a></h4>';
	basiclti_filter_print_types();
	echo '</fieldset>';
	echo '<div class="mdl-align"><a href="'.$CFG->wwwroot.'/admin/settings.php?section=modsettingbasiclti">'.get_string('back').'</a></div>';
}

print_footer();

?>
